package com.allwees.bs.module.oms.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.allwees.bs.module.lms.vo.TrackBody;
import com.allwees.bs.module.lms.vo.TrackDetails;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 物流信息回调
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/9/19 11:05
 */
@PrintLog
@Slf4j
@RestController
public class TrackingWebhookController {

    private static final Gson GSON = new GsonBuilder().setLenient().create();

    private static final String SUCCESS = "200";

    private static final String CONTINUE = "continue";
    @Autowired
    private GeneralOmsOrderItemService generalOmsOrderItemService;

    /**
     * 51tracking回调
     * @param body
     * @return
     */
    @AnonymousAccess
    @PostMapping("/track/callback/pre")
    public String trackingCallback(@RequestBody String body) {
        log.info("===>tracking callback:{}", body);
        TrackDetails details = GSON.fromJson(body, TrackDetails.class);
        if(!details.isDelivered()){
            return CONTINUE;
        }
        TrackBody trackBody = details.getData();
        String expressCode = trackBody.getTracking_number();
        String carrierCode = trackBody.getCarrier_code();
        generalOmsOrderItemService.doDelivered(expressCode,carrierCode);
        return SUCCESS;
    }
}
